package com.zc.demo.controller;

import com.zc.demo.model.UserInfo;
import com.zc.demo.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //登录接口
    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){
        System.out.println(userName);
        System.out.println(password);
        if(!StringUtils.hasLength(userName) && !StringUtils.hasLength(password)){
            //信息为空，返回false
            return false;
        }
        //然后从数据库中匹配

        UserInfo userInfo = userService.queryByName(userName);
        if(userInfo == null){
            return false;
        }
        System.out.println(userName);
        System.out.println(password);
        //验证账号和密码
        if(userInfo != null && password.equals(userInfo.getPassword())){
            //保存在Session
            userInfo.setPassword("");
            session.setAttribute("seesion_key_user",userInfo);
            return true;
        }
        return false;
    }
}
